//
//  UDRefreshControl.swift
//  AutoPrint
//
//  Created by 胡玉葱 on 16/9/28.
//  Copyright © 2016年 fatcat. All rights reserved.
//  下拉刷新控制器

import UIKit

enum LLRefreshControllerState {
    case Normal
    case Ready
    case Refreshing
}

class LLRefreshControl: UIView {
    
    var titleLabel: UILabel!
    private var normalText = "下拉刷新"
    private var readyText = "松开刷新"
    private var refreshingText = "正在刷新"
    var normalTitle: String{
        get{
            return normalText
        }
        set(val){
            normalText = val
            if state == .Normal{
                titleLabel.text = val
            }
        }
    }
    var readyTitle: String{
        get{
            return readyText
        }
        set(val){
            readyText = val
            if state == .Ready{
                titleLabel.text = val
            }
        }
    }
    var refreshingTitle: String{
        get{
            return refreshingText
        }
        set(val){
            refreshingText = val
            if state == .Refreshing{
                titleLabel.text = val
            }
        }
    }
    var state: LLRefreshControllerState = .Normal
    
    override init(frame: CGRect){
        super.init(frame: frame)
        titleLabel = UILabel(frame: CGRect(x: 16, y: 0, width: frame.width - 32, height: frame.height))
        titleLabel.center = CGPoint(x: frame.width/2, y: frame.height/2)
        titleLabel.textAlignment = .center
        titleLabel.textColor = UIColor.lightGray
        titleLabel.font = UIFont.systemFont(ofSize: 12)
        titleLabel.text = normalText
        addSubview(titleLabel)
    }
    
    func readyToRefresh(){
        state = .Ready
        titleLabel.text = readyText
    }
    
    func beginRefresh(){
        state = .Refreshing
        titleLabel.text = refreshingText
    }
    
    func endRefresh(){
        state = .Normal
        titleLabel.text = normalText
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */

}
